/**
* @author madStone_l
*/
Ext.define('Sys.ux.GridPanel', {
    extend: 'Ext.grid.Panel',
    // defines a new xtype 'myclass'(like in 3.x)
    alias: 'widget.sysgridpanel',
    requires: ['Sys.ux.JsonStore', 'Sys.ux.SearchField', 'Sys.ux.PageSizeChange'],
    config: {
        showRefreshBtn: false,
        showDelBtn: false,
        findBtn: false,
        exExcel: false,
        search: false,
        loadDataRight: true,
        paramsConfig: {
            cmd: 'cmd',
            'delete': 'delete',
            update: 'update',
            select: 'getList',
            insert: 'insert',
            jsonText: 'jsonText'
        }
    },
    applySearch: function () {

    },
    applyShowRefreshBtn: function () {

    },
    constructor: function () {
        // alert('父类构造函数')
        // if (!Ext.isEmpty(this.store) || !Ext.isEmpty(this.storeConfig)) {
        // if (!Ext.isEmpty(this.storeConfig))
        // this.store = Ext.create('Sys.ux.JsonStore', this.storeConfig);
        // }
        // debugger;
        // if (Ext.type(this.store) == "string")
        // this.store = Ext.widget(this.store, this.storeConfig);

        if (!this.bbar && this.store.pageSize) {
            this.bbar = Ext.create('Ext.PagingToolbar', {
                store: this.store,
                displayInfo: true,
                items: this.bbarItems
            });
        }
        if (this.showTbar !== false) {
            if (this.showDelBtn || this.showRefreshBtn || this.search
					|| this.findBtn)
                this.tbar = this.tbar || [];
            if (this.findBtn === true) {
                this.tbar.push('-');
                this.tbar.push({
                    text: '查询',
                    action: 'find',
                    iconCls: 'icon_find',
                    scope: this,
                    handler: this.find
                });
                this.tbar.push({
                    text: '重置',
                    action: 'clear',
                    iconCls: 'icon_clear',
                    scope: this,
                    handler: this.clear
                });
            }
            if (this.showRefreshBtn === true)
                this.tbar.push({
                    text: '刷新',
                    iconCls: 'icon_refresh',
                    scope: this,
                    handler: function () {
                        this.getStore().reload();
                    }
                });
            if (this.showDelBtn === true)
                this.tbar.push('-', {
                    text: '删除',
                    iconCls: 'icon_remove',
                    scope: this,
                    handler: function () {
                        var s = this.checkSelect(true);
                        if (s) {
                            Ext.Msg.confirm('操作确认', '确定删除当前选中的数据？', function (
									btn) {
                                if (btn == 'yes'
										&& this.fireEvent('afterdelete', this,
												s) !== false) {
                                    var url = this.getStore().url;
                                    if (Ext.isEmpty(url)) {
                                        this.store.remove(s);
                                        this.getView().refresh();
                                        Ext.Msg.hide();
                                        Sys.Msg.showInfo('删除成功！');
                                    }

                                    var i = url.indexOf('?');
                                    if (i == -1)
                                        url += "?" + this.paramConfig.cmd + "="
												+ this.paramConfig['delete'];
                                    else {
                                        // 解析URL参数
                                        var params = Ext.urlDecode(url
												.substring(i + 1));
                                        // 设置删除命令
                                        params[this.paramConfig.cmd] = this.paramConfig['delete'];
                                        // 重组URL
                                        url = url.substring(0, i + 1)
												+ Ext.urlEncode(params);
                                    }
                                    var r = Ext.isArray(s) ? s[0] : s;
                                    var cmdParams = {}
                                    cmdParams[this.paramConfig.jsonText] = Ext
											.encode(r.data);
                                    Ext.Ajax.request({
                                        url: url,
                                        params: Ext.apply(cmdParams, r.data),
                                        success: function (response, opts) {
                                            var obj = Ext
													.decode(response.responseText);
                                            if (obj.success) {
                                                this.store.remove(s);
                                                this.getView().refresh();
                                                Ext.Msg.hide();
                                                Sys.Msg.showInfo('删除成功！');
                                            } else if (!obj.success) {
                                                Ext.Msg.showError('删除失败：'
														+ obj.msg);
                                            }
                                        },
                                        failure: function (response, opts) {
                                            Ext.Msg.showRError(response, opts,
													'删除');
                                        },
                                        scope: this
                                    });
                                }
                            }, this);
                        }
                    }
                });
            if (this.exExcel === true) {
                this.tbar.push('-', {
                    text: '导出数据',
                    iconCls: 'icon_xlsx',
                    menu: [{
                        text: '当前页',
                        iconCls: 'icon_xlsx',
                        scope: this,
                        handler: function () {
                            Sys.fn.exportToExcel(this, false);
                        }
                    }, {
                        text: '全部数据',
                        iconCls: 'icon_xlsx',
                        scope: this,
                        handler: function () {
                            Sys.fn.exportToExcel(this, true);
                        }
                    }]
                });
            }
            if (this.search === true) {
                this.tbar.push('->', Ext.create('Sys.ux.SearchField', {
                    store: this.store
                }));
            }
        }
        this.callParent(arguments);
    },
    initComponent: function () {
        this.callParent(arguments);
        if (this.getStore().pageSize > 0) {
            this.addDocked(Ext.create('Ext.PagingToolbar', {
                plugins: new Sys.ux.PageSizeChange(),
                dock: 'bottom',
                store: this.store,
                displayInfo: true,
                items: this.bbarItems
            }));
            // this.doLayout();
        }
    },
    checkSelect: function (isAlert) {
        var sm = this.getSelectionModel();
        var rs = sm.getSelection();
        if (rs.length == 0) {
            if (isAlert === true)
                Ext.Msg.showNotSelected();
            return false;
        }
        switch (sm.mode) {
            case 'SINGLE': // 单选模式
            case 'SIMPLE': // 简单模式
                return rs[0];
                break;
            case 'MULTI': // 多选模式
                return rs;
                break;
        }

    },
    statics: {
        method: function () {
            return 'abc';
        }
    }
});